package electrycity;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class EleAPP2 {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        //建立job对象
        job.setJarByClass(EleAPP2.class);
        job.setJobName("task2");
        //设置驱动类
        job.setMapperClass(EleMapper.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(ElectricWritable.class);
        //设置mapper类
        job.setReducerClass(EleReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(ElectricWritable.class);
        //设置reducer类
        FileInputFormat.setInputPaths(job, new Path("file/electricity/electric.txt"));
        FileOutputFormat.setOutputPath(job, new Path("file/electricity/out/task2"));
        //设置输入输出文件路径
        job.waitForCompletion(true);
        //提交程序
    }

    public static class EleMapper extends Mapper<LongWritable, Text, Text, ElectricWritable> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, ElectricWritable>.Context context)
                throws IOException, InterruptedException {
            String[] split = value.toString().split(";");
            if (split.length == 11) {
                String year = split[2];
                String cf = split[8];
                String xyf = split[9];
                String ktf = split[10];
                if (!(year.equals("?") || cf.equals("?") || xyf.equals("?") || ktf.equals("?"))) {
                    ElectricWritable ele = new ElectricWritable
                            (new Double(cf), new Double(xyf), new Double(ktf), 0.0);
                    context.write(new Text(year), ele);
                }
            }
        }
    }

    public static class EleReducer extends Reducer<Text, ElectricWritable, Text, ElectricWritable> {
        @Override
        protected void reduce(Text key, Iterable<ElectricWritable> values, Reducer<Text, ElectricWritable, Text, ElectricWritable>.Context context)
                throws IOException, InterruptedException {
            double cfsum = 0;
            double xysum = 0;
            double ktsum = 0;
            double sum = 0;
            for (ElectricWritable value : values) {
                cfsum += value.getCf();
                xysum += value.getXy();
                ktsum += value.getKt();
            }
            sum += cfsum + xysum + ktsum;
            ElectricWritable ele = new ElectricWritable();
            ele.setCf(cfsum);
            ele.setXy(xysum);
            ele.setKt(ktsum);
            ele.setSum(sum);
            context.write(key, ele);
        }
    }
}
